var two_star_json = {};

$(function () {
    //二星定位 数字
    $('.select_number_box span').on('click', function () {
        var that = $(this);

        that.parent().attr('data-select', '1');

        if (count_unit_number() > 2) {
            set_alert('tip', '只能选择两种单位');
            that.parent().attr('data-select', '0');
            return false;
        }


        if (!that.hasClass('select')) {
            var unit = $(this).parents('.two_star_box').attr('data-unit');
            var num = two_star_json[unit] ? two_star_json[unit] : [];
            num.push($(this).attr('data-number'));
            two_star_json[unit] = num;
        }
        that.addClass('select');
        that.parent().attr('data-select', '1');
        // console.log(two_star_json)

        count_draw_number();
        count_total_price($('input[name=ave_price]').val(), $('#draw_num').html());
    });
    //文字
    $('.select_number_box label').on('click', function () {
        var that = $(this);

        that.parent().attr('data-select', '1');

        if (count_unit_number() > 2) {
            set_alert('tip', '只能选择两种单位');
            that.parent().attr('data-select', '0');
            return false;
        }
        var unit = $(this).parents('.two_star_box').attr('data-unit');

        var area = that.data('area');
        if (area == 'all') {
            that.parent().find('span').addClass('select');
            that.parent().attr('data-select', '1');
            delete two_star_json[unit];
            var dom_list = that.parent().find('span.select');

            var tmp = [];
            dom_list.each(function (i, n) {
                tmp.push($(n).data('number'));
            });
            two_star_json[unit] = tmp;
        } else if (area == 'clear') {
            delete two_star_json[unit];
            that.parent().find('span').removeClass('select');
            that.parent().attr('data-select', '0');
        } else {
            // that.parent().find("span[data-" + area + "=1]")
            that.parent().find('span').removeClass('select');
            that.parent().attr('data-select', '0');

            that.parent().find("span[data-" + area + "=1]").addClass('select');
            that.parent().attr('data-select', '1');
            delete two_star_json[unit];
            var dom_list = that.parent().find('span.select');

            var tmp = [];
            dom_list.each(function (i, n) {
                tmp.push($(n).data('number'));
            });
            two_star_json[unit] = tmp;
        }

        count_draw_number();
        count_total_price($('input[name=ave_price]').val(), $('#draw_num').html());
    });
    //清空
    $('.clear_all_btn').on('click', function () {
        two_star_json = {};
        $('.select_number_box span').removeClass('select');
    });
    //随机
    $('.random_btn').on('click', function () {
        $('.select_number_box span').removeClass('select');

        //随机生成两个数
        var index_arr = [0, 1, 2, 3];
        var random_1 = getRandom(0, 3);
        var unit_index_1 = index_arr[random_1];
        index_arr.splice(random_1, 1);

        var random_2 = getRandom(0, 2);
        var unit_index_2 = index_arr[random_2];

        var number_1 = getRandom(0, 9);
        var number_2 = getRandom(0, 9);

        $('.select_number_box').eq(unit_index_1).find('span').eq(number_1).addClass('select');
        $('.select_number_box').eq(unit_index_2).find('span').eq(number_2).addClass('select');


        two_star_json = {};
        var temp = [];
        var o_1 = $('.select_number_box').eq(unit_index_1);
        var o_2 = $('.select_number_box').eq(unit_index_2);
        var unit_1 = o_1.parent().data('unit');
        var unit_2 = o_2.parent().data('unit');


        temp.push(o_1.find('span.select').data('number'));
        two_star_json[unit_1] = temp;
        temp = [];
        temp.push(o_2.find('span.select').data('number'));
        two_star_json[unit_2] = temp;

        count_draw_number();
        count_total_price($('input[name=ave_price]').val(), $('#draw_num').html());
    });

    //输入金额
    $('input[name=ave_price]').on('keyup', function () {
        var that = $(this);
        if (isNaN(that.val())) {
            that.val('');
        }
        count_total_price(that.val(), $('#draw_num').html());
    });


});


//统计总金额
function count_total_price(draw_number, ave_price) {
    $('#total_price').html(parseInt(draw_number) * parseInt(ave_price));
}

//统计选择的单位数
function count_unit_number() {
    // console.log('unit_num:' + $('.select_number_box[data-select=1]').length);
    return $('.select_number_box[data-select=1]').length;
}

//计算注数
function count_draw_number() {
    var num = 0;
    $('.select_number_box').each(function () {
        var that = $(this);
        var cur_length = 0;
        that.find('span').each(function () {
            if ($(this).hasClass('select')) {
                cur_length++;
            }
        });
        // console.log('length：' + cur_length);
        //只统计两组
        if (num == 0) {
            num = cur_length;
        } else {
            num = num * cur_length;
            return false;
        }
    })
    $('#draw_num').html(num);
}


$('.two_star_draw_btn').click( function (data) {
    // console.log(two_star_json)
    var price = $('input[name=ave_price]').val();
    if (!price || price == '0') {

        set_alert('tip', '填入正确的金额');
        return false;
    }
    var lottery_data = {
        list: two_star_json,
        price: price,
        round_turn: round_turn,
        room_id: room_uuid,

    };
    if (seconds <= 60) {
        set_alert('tip', '倒计时60s内不能投注');
        return false;
    }

    if (!IS_ENTER) {
        set_alert('tip', '请点击空位坐下');
        return false;
    }
    request(GV.url + 'game/addTwoStarBet', lottery_data, function (json) {
        if (json.ResponseStatus == 0) {

            requestUserInfo();
            set_alert('tip', '投注成功');
            $('.clear_all_btn').trigger('click');

        } else {

            set_alert('tip', json.ResponseMsg);

        }
    });


});
